﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AAA.DesignPattern.Observer;
using AAA.TradeLanguage.Data;
using AAA.Trade.SignalCatcher;

namespace AAA.TradeLanguage
{
    public class TrackingCenter : SignalCatcher, IObserver, ISubject
    {
        private Subject _subject;

        public TrackingCenter()
        {
            _subject = new Subject();
        }


        #region IObserver Members

        public void Update(object oSource, IMessageInfo miMessage)
        {
            AAA.TradeLanguage.Data.SignalInfo receiveSignalInfo = (AAA.TradeLanguage.Data.SignalInfo)miMessage.Message;
            AAA.Meta.Trade.Data.SignalInfo tradeSignalInfo = new AAA.Meta.Trade.Data.SignalInfo();            

            tradeSignalInfo.Signal = receiveSignalInfo.SignalName;
            tradeSignalInfo.OrderType = receiveSignalInfo.OrderType.ToString();
            tradeSignalInfo.TimePlaced = receiveSignalInfo.TimePlaced;
            tradeSignalInfo.FillPrice = receiveSignalInfo.Price.ToString("0.00");
            tradeSignalInfo.EntryPrice = receiveSignalInfo.Price.ToString("0.00");
            tradeSignalInfo.EntryTime = receiveSignalInfo.TimePlaced;            

            switch (miMessage.MessageSubject)
            {
                case "Filled":
                    if (OnFilledOrderEvent != null)
                        OnFilledOrderEvent(tradeSignalInfo);
                    break;

                case "Active":
                    if (OnActiveOrderEvent != null)
                        OnActiveOrderEvent(tradeSignalInfo);
                    break;

                case "Canceled":
                    if (OnCanceledOrderEvent != null)
                        OnCanceledOrderEvent(tradeSignalInfo);
                    break;

                case "Open":
                    if (OnOpenPositionEvent != null)
                        OnOpenPositionEvent(tradeSignalInfo);
                    break;
            }

            Notify(miMessage);
        }

        #endregion

        #region ISubject Members

        public void Attach(IObserver observer)
        {
            _subject.Attach(observer);
        }

        public void Detach(IObserver observer)
        {
            _subject.Detach(observer);
        }

        public void Notify(IMessageInfo miMessage)
        {
            _subject.Notify(miMessage);
        }

        #endregion
    }
}
